﻿using System;
using System.Web;
using Microsoft.Practices.Unity;

namespace Yeap.Kernel.Unity
{
    public class PerThreadLifetimeManager<T> : LifetimeManager
    {
        [ThreadStatic]
        private static object value;
        private static readonly string key = Guid.NewGuid().ToString();

        public override object GetValue()
        {
            if (HttpContext.Current != null && HttpContext.Current.Items.Contains(key))
            {
                return HttpContext.Current.Items[key];
            }
            return value;
        }

        public override void SetValue(object newValue)
        {
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Items[key] = newValue;
            }
            value = newValue;
        }
        public override void RemoveValue()
        {
        }
    }

}